
package com.bojin.studio.ubattery;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.IBinder;

public class NotifyingService extends Service { 
	
     private NotificationManager mNotificationManager;
	 private PendingIntent mPendingIntent;
	 private Notification mNotification;
	 Context mcontext;
	 
	 private BroadcastReceiver mBatteryInfoReceiver=new BroadcastReceiver()
	 {
	  @Override
	  public void onReceive(Context context, Intent intent) {
	   // TODO Auto-generated method stub		 
	   String action=intent.getAction();
	   mcontext=context;
	   if(Intent.ACTION_BATTERY_CHANGED.equals(action))
	   {	 	  
          updateStatusBar(intent);    
	   }
	  } 
	 };
	 
    @Override
    public void onCreate() {
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mPendingIntent=PendingIntent.getActivity(NotifyingService.this, 0, new Intent(this, batteryActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        mNotification=new Notification();  
        registerReceiver(mBatteryInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    @Override
    public void onDestroy() {
    	unregisterReceiver(mBatteryInfoReceiver);
    	mNotificationManager.cancelAll();
    }

    @Override
    public IBinder onBind(Intent intent) {
    	return null;
    }
    
    private String statusText(int mStatus)
    {
    	String nStatus="";
    	 switch(mStatus)
 	    {
 	     case 1:
 	    	 nStatus=getResources().getString(R.string.unknown);
 	     break;
 	     case 2:
 	    	 nStatus=getResources().getString(R.string.charging);
 		 break;
 	     case 3:
 	    	 nStatus=getResources().getString(R.string.discharging);
 		 break;
 	     case 4:
 	    	 nStatus=getResources().getString(R.string.ncharging);
 		 break;
 	     case 5:
 	    	 nStatus=getResources().getString(R.string.bfull);
 	     break;
 	    }
    	 return nStatus;
    }
    
    void updateStatusBar(Intent intent)
    {
    	int mLevel=intent.getIntExtra("level",0);
	 	int mScale=intent.getIntExtra("scale",100); 
	 	int cLevel=mLevel*100/mScale;
	 	int mStatus = intent.getIntExtra("status", 0);   
 	    int mVoltage = intent.getIntExtra("voltage", 0);
 	    int mTemperature = intent.getIntExtra("temperature", 0);
 	    String nStatus=statusText(mStatus); 
        int tempC=mTemperature/10;
        //int tempF=(tempC*9/5)+32;
       /* try
        {
                batteryDB batt_db=new batteryDB(mcontext);
        	    Cursor cr=batt_db.select();
        	    batt_db.insert(Long.toString(System.currentTimeMillis()), Integer.toString(mLevel*100/mScale), Double.toString(mVoltage/1000.0), Integer.toString(tempC), nStatus);
        	    if(cr.getCount()>1000);
        	    {      	    	
        	    	for(cr.moveToFirst();!(cr.isAfterLast());cr.moveToNext())
        	    	{    		
        	    	 batt_db.delete(cr.getPosition());
        	    	} 
        	    }
        	    cr.close();
        	    batt_db.close();   
         }
         catch(Exception e)
         {
   	   
         }*/
         mNotification.flags|=Notification.FLAG_NO_CLEAR;
    	 mNotification.icon=R.drawable.k+cLevel; 	   
	     mNotification.contentIntent=mPendingIntent;  
	     mNotification.setLatestEventInfo(NotifyingService.this, getResources().getString(R.string.b_level)+" : "+String.valueOf(cLevel)+"%",getResources().getString(R.string.getInfo), mPendingIntent);
	     mNotificationManager.notify(0,mNotification);
    }
}
